import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * authorityCompany - 租户级权限维护tab页 - 公司 - model
 * @date: 2018-7-31

 */
import { getResponse } from 'utils/utils';
import { queryCompany, updateCompany } from "../../services/authorityManagementService";
export default {
  namespace: 'authorityCompany',
  state: {
    data: [],
    checkList: [],
    originList: [],
    expandedRowKeys: []
  },
  effects: {
    fetchAuthorityCompany: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAuthorityCompany(_ref, _ref2) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchAuthorityCompany$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryCompany, payload);

            case 4:
              response = _context.sent;
              data = getResponse(response);

              if (!data) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'queryCompany',
                payload: data
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchAuthorityCompany);
    }),
    fetchAuthorityCompanyAndExpand: /*#__PURE__*/_regeneratorRuntime.mark(function fetchAuthorityCompanyAndExpand(_ref3, _ref4) {
      var payload, call, put, response, data, expandedRowKeys;
      return _regeneratorRuntime.wrap(function fetchAuthorityCompanyAndExpand$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(queryCompany, payload);

            case 4:
              response = _context2.sent;
              data = getResponse(response);

              if (!data) {
                _context2.next = 12;
                break;
              }

              expandedRowKeys = data.originList && data.originList.map(function (list) {
                return list.id;
              });
              _context2.next = 10;
              return put({
                type: 'queryCompany',
                payload: data
              });

            case 10:
              _context2.next = 12;
              return put({
                type: 'updateExpanded',
                payload: expandedRowKeys
              });

            case 12:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchAuthorityCompanyAndExpand);
    }),
    updateAuthorityCompany: /*#__PURE__*/_regeneratorRuntime.mark(function updateAuthorityCompany(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function updateAuthorityCompany$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(updateCompany, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateAuthorityCompany);
    })
  },
  reducers: {
    queryCompany: function queryCompany(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: action.payload.treeList,
        checkList: action.payload.originList.filter(function (list) {
          return list.checkedFlag === 1;
        }),
        originList: action.payload.originList
      });
    },
    updateCheckList: function updateCheckList(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        checkList: action.payload
      });
    },
    updateExpanded: function updateExpanded(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        expandedRowKeys: action.payload
      });
    },
    updateState: function updateState(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};